<template>
  <XtxDialog :title="`${formData.id?'编辑':'添加'}收货地址`" v-model:visible="dialogVisible">
    <div class="address-edit">
      <div class="xtx-form">
        <div class="xtx-form-item">
          <div class="label">收货人：</div>
          <div class="field">
            <input v-model="formData.receiver" class="input" placeholder="请输入收货人" />
          </div>
        </div>
      <div class="xtx-form-item">
        <div class="label">手机号：</div>
        <div class="field">
          <input v-model="formData.contact" class="input" placeholder="请输入手机号" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">地区：</div>
        <div class="field">
          <XtxCity :fullLocation="formData.fullLocation" @change="changeCity" placeholder="请选择收货地址"/>
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">详细地址：</div>
        <div class="field">
          <input v-model="formData.address" class="input" placeholder="请输入详细地址" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">邮政编码：</div>
        <div class="field">
          <input v-model="formData.postalCode" class="input" placeholder="请输入邮政编码" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">地址标签：</div>
        <div class="field">
          <input v-model="formData.addressTags" class="input" placeholder="请输入地址标签，逗号分隔" />
        </div>
      </div>
      </div>
    </div>
    <template v-slot:footer>
      <XtxButton @click="dialogVisible=false" type="gray" style="margin-right:20px">取消</XtxButton>
      <XtxButton @click="submit()" type="primary">确认</XtxButton>
    </template>
  </XtxDialog>
</template>

<script>
import { reactive, ref } from 'vue'
import { addAddress, editAddress } from '@/api/order'
import Message from '@/components/library/message'
export default {
  name: 'AddressEdit',
  setup (props, { emit }) {
    const dialogVisible = ref(false)
    // 定义一个open函数，作为打开对话框函数(组件实例拥有open函数)
    const open = (address) => {
      // 如果是编辑，先需要向表单中填充需要修改的原始数据
      if (address.id) {
        for (const key in address) {
          formData[key] = address[key]
        }
      } else {
        // 如果添加 清空表单
        for (const key in formData) {
          if (key === 'isDefault') {
            formData[key] = 1
          } else {
            formData[key] = null
          }
        }
      }
      dialogVisible.value = true
    }

    // 定义表单数据对象
    const formData = reactive({
      receiver: null, // 收件人
      contact: null, // 联系方式
      provinceCode: null, // 所在省份编码
      cityCode: null, // 所在城市编码
      countyCode: null, // 所在区/县编码
      address: null, // 详细地址
      postalCode: null, // 邮政编码
      addressTags: null, // 地址标签
      isDefault: 1, // 是否默认地址0为是，1为否
      // 城市组件显示文字（完整行政区地址）
      fullLocation: null,
      id: null
    })
    // 城市选择
    const changeCity = (result) => {
      formData.provinceCode = result.provinceCode
      formData.cityCode = result.cityCode
      formData.countyCode = result.countyCode
      formData.fullLocation = result.fullLocation
    }
    // 添加时候提交操作
    const submit = () => {
      if (formData.id) {
        // 修改收货地址请求
        editAddress(formData).then(data => {
          // 修改成功
          Message({ type: 'success', text: '修改收货地址成功' })
          dialogVisible.value = false
          emit('on-success', formData)
        })
      } else {
        // 1.省略了校验
        // 2.发送添加收货地址请求
        addAddress(formData).then(data => {
          // 需要设置ID
          formData.id = data.result.id
          Message({ type: 'success', text: '添加收货地址成功' })
          // 关闭对话框
          dialogVisible.value = false
          // 需要给结算组件地址列表中(加一条地址)
          // 触发自定义事件
          emit('on-success', formData)
        })
      }
    }
    return { dialogVisible, open, formData, changeCity, submit }
  }
}
</script>

<style lang="less" scoped>
.xtx-dialog {
  :deep(.wrapper){
    width: 780px;
    .body {
      font-size: 14px;
    }
  }
}
.xtx-form {
  padding: 0;
  input {
    outline: none;
    &::placeholder {
      color: #ccc;
    }
  }
}
.xtx-city {
  width: 320px;
  :deep(.select) {
    height: 50px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;
    .placeholder {
      color: #ccc;
    }
    i {
      color: #ccc;
      font-size: 18px;
    }
    .value {
      font-size: 14px;
    }
  }
  :deep(.option) {
    top: 49px;
  }
}
</style>
